/*
 * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.xuexiang.xui_lib.tabbar.vertical;

import ohos.agp.components.Component;
import ohos.agp.components.StackLayout;
import ohos.app.Context;

import com.xuexiang.xui_lib.component.textview.badge.BadgeView;

/**
 * 选项卡上的圆点小标记
 *
 * @author xuexiang
 * @since 2018/12/28 下午1:34
 */
public class TabBadgeView extends BadgeView {
    private TabBadgeView(Context context) {
        super(context);
    }

    public static TabBadgeView bindTab(TabView tab) {
        TabBadgeView badge = null;
        for (int i = 0; i < tab.getChildCount(); i++) {
            Component child = tab.getComponentAt(i);
            if (child instanceof TabBadgeView) {
                badge = (TabBadgeView) child;
                break;
            }
        }
        if (badge == null) {
            badge = new TabBadgeView(tab.getContext());
            tab.addComponent(badge, new TabView.LayoutConfig(
                TabView.LayoutConfig.MATCH_PARENT,
                TabView.LayoutConfig.MATCH_PARENT));
        }
        badge.mTargetView = tab;
        return badge;
    }

    @Override
    protected void screenFromWindow(boolean screen) {
        if (getComponentParent() != null) {
            getComponentParent().removeComponent(this);
        }
        if (screen) {
            mActivityRoot.addComponent(this, new StackLayout.LayoutConfig(
                StackLayout.LayoutConfig.MATCH_PARENT,
                StackLayout.LayoutConfig.MATCH_PARENT));
        } else {
            if (mTargetView instanceof TabView) {
                ((TabView) mTargetView).addComponent(this,
                    new TabView.LayoutConfig(TabView.LayoutConfig.MATCH_PARENT,
                        TabView.LayoutConfig.MATCH_PARENT));
            } else {
                bindTarget(mTargetView);
            }
        }
    }
}
